package com.gzhu.knowledgeAdmin.service.impl;

import com.gzhu.knowledgeAdmin.common.utils.CustomMultipartFile;
import com.gzhu.knowledgeAdmin.common.utils.GoFastDfsUtil;
import com.gzhu.knowledgeAdmin.common.utils.UserInfoUtils;
import com.gzhu.knowledgeAdmin.common.vo.GoFastDfsUploadFileVo;
import com.gzhu.knowledgeAdmin.dao.AVideoMapper;
import com.gzhu.knowledgeAdmin.entity.AVideo;
import com.gzhu.knowledgeAdmin.service.AVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;

@Service
public class AVideoServiceImpl implements AVideoService {
    @Autowired
    private AVideoMapper aVideoMapper;
    @Autowired
    private GoFastDfsUtil goFastDfsUtil;

    public static MultipartFile convertFileToMultipartFile(File file) throws IOException {
        return new CustomMultipartFile(file);
    }
    //音频上传
    @Override
    public String uploadFile(MultipartFile file) throws IOException, URISyntaxException {
        //文件路径处理
        String gsid = UserInfoUtils.getCurrentUser().getGsid();
        String filepath="QI"+"/"+gsid;

        //文件名处理
        String fileName = file.getOriginalFilename();
        GoFastDfsUploadFileVo goFastDfsUploadFileVo=goFastDfsUtil.uploadFileAVdio(file,fileName,filepath);
        String username = UserInfoUtils.getCurrentUser().getUserName();
        if(aVideoMapper.selectOneBywjm(fileName)==null){
            aVideoMapper.insertAudio(fileName,username);
        }
        return goFastDfsUploadFileVo.getUrl();
    }

    @Override
    public List<AVideo> select(AVideo aVideo) {
       return  aVideoMapper.selectList(aVideo);
    }

    //视频上传
    @Override
    public String VUpload(MultipartFile file) throws IOException, URISyntaxException {


        //文件路径处理
        String gsid = UserInfoUtils.getCurrentUser().getGsid();
        String filepath="QI"+"/"+gsid;

        String fileName = file.getOriginalFilename();
        GoFastDfsUploadFileVo goFastDfsUploadFileVo=goFastDfsUtil.uploadFileAVdio(file,fileName,filepath);
        String username = UserInfoUtils.getCurrentUser().getUserName();
        if(aVideoMapper.selectOneBywjm(fileName)==null){
            aVideoMapper.insertVideo(fileName,username);
        }
        return goFastDfsUploadFileVo.getUrl();

    }

    @Override
    public void exportAVideo(AVideo aVideo, HttpServletResponse response) throws MalformedURLException, URISyntaxException {
        String gsid = UserInfoUtils.getCurrentUser().getGsid();
        if(aVideo.getSctype()!=null||aVideo.getSctype()!=""){
        goFastDfsUtil.downloadFile(goFastDfsUtil.getServicePath() + '/' + "QI/"+gsid+"/"+aVideo.getWjm(), response);
        }


    }

    @Override
    public String filescan() throws IOException, URISyntaxException {
        // 指定文件夹路径
        String directoryPath = "D:\\wav";
        String directoryPath1 = "D:\\mp4";

        // 创建File对象
        File directory = new File(directoryPath);
        File directory1 = new File(directoryPath1);

        // 检查目录是否存在且为目录
        if (!directory.exists() || !directory.isDirectory()) {
            throw  new IOException("指定路径不存在或不是一个目录");
        }
        if (!directory1.exists()||!directory1.isDirectory()){
            throw  new IOException("指定路径不存在或不是一个目录");
        }

        // 获取所有.wav文件
        File[] warFiles = directory.listFiles((dir, name) -> name.toLowerCase().endsWith(".wav"));

        File[] mp4Files = directory1.listFiles((dir, name) -> name.toLowerCase().endsWith(".mp4"));
        // 检查是否有.war文件
        if (warFiles == null || warFiles.length == 0) {
            throw  new IOException("文件路径中的wav文件为空");
        }
        if (mp4Files==null||mp4Files.length==0){
            throw  new IOException("文件路径中的mp4文件为空");
        }

        for (File file : warFiles) {
            MultipartFile multipartFile =convertFileToMultipartFile(file);

            System.out.println("找到的.wav文件: " + multipartFile.getOriginalFilename());
            uploadFile(multipartFile);
        }
        for (File file:mp4Files){
            MultipartFile multipartFile =convertFileToMultipartFile(file);

            System.out.println("找到的.mp4文件: "+multipartFile.getOriginalFilename());
            VUpload(multipartFile);
        }
        return "成功";
    }
}
